const Request = function(url, data, method = 'POST', isFrom = false, isLoading = true, tempToken) {
	return new Promise((resolve, reject) => {
		const token = uni.getStorageSync('token');
		const userCode = uni.getStorageSync('userCode');
		let headers;
		if(isFrom == true) {
			headers = 'application/x-www-form-urlencoded';
		}else{
			headers = 'application/json';
		}
		if (isLoading) {
			uni.showLoading({
				title: '加载中',
				mask: true
			})
		}

		uni.request({
			url,
			method,
			data,
			header: {
				'content-type': headers,
				"x-requested-with": "XMLHttpRequest",
				'token': token,
				'userCode': userCode
			},
			success(res) {
				if (isLoading) uni.hideLoading();
				if (res.statusCode === 200) {
					let {
						data: {
							code
						}
					} = res;
					if (code == 0 || code == 2000) {
						resolve(res);
					} else if (code == -2) {
						uni.navigateTo({
							url: '/pages/login/login'
						})
						reject(res)
					} else {
						if(res.data.msg){
							uni.showToast({
								title: res.data.msg,
								icon: "none",
								duration: 2000,
							})
						}else{
							uni.showToast({
								title: res.data.message,
								icon: "none",
								duration: 2000,
							})
						}
						reject(res)
					}
				} else {
					switch (res.statusCode) {
						case 500:
							uni.showToast({
								title: "500:服务器内部错误",
								icon: "none",
								duration: 2000
							})
							break;
						case 404:
							uni.showToast({
								title: "404:网络请求不存在",
								icon: "none",
								duration: 2000

							})
							break;
						case 403:
							uni.showToast({
								title: "403:网络请求不存在",
								icon: "none",
								duration: 2000

							})
							break;
						default:
							uni.showToast({
								title: '服务器繁忙，请稍后再试',
								icon: "none",
								duration: 2000,

							})
					}
					reject(res);
				}
			},
			fail(err) {
				if (isLoading) uni.hideLoading();
				uni.showToast({
					title: '网络异常',
					icon: 'none'
				})
				reject(err);
			}
		})
	})
}
export default Request
